<template>
  <a-drawer
    title="修改检查条目"
    :maskClosable="false"
    width=650
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="entryEditVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label='条目编号' v-bind="formItemLayout">
        <a-input v-decorator="['entryNum', {rules: [ {required: true, message: '条目编号不能为空'}]}]" style="width: 100%"/>
      </a-form-item>
      <a-form-item label='所属单元' v-bind="formItemLayout">
        <a-select
          showSearch
          placeholder="请选择所属单元"
          optionFilterProp="children"
          style="width: 200px"
          :filterOption="filterOption"
          v-decorator="['unitId',{rules: [{ required: true, message: '请选择所属单元' }]}]">
          <a-select-option v-for="u in unitData" :key="u.unitId">{{u.unitName}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='检查内容' v-bind="formItemLayout">
        <a-textarea
          :rows="5"
          v-decorator="[
          'entryContent',
          {rules: [
            { max: 225, message: '长度不能超过225个字符'}
          ]}]">
        </a-textarea>
      </a-form-item>
      <a-form-item label="检查依据" v-bind="formItemLayout">
          <a-input v-decorator="['entryBasis']"/>
      </a-form-item>
      <a-form-item label='分数' v-bind="formItemLayout">
        <a-input-number v-decorator="['entryScore']" style="width: 100%"/>
      </a-form-item>
      <a-form-item label='是否为否决项' v-bind="formItemLayout">
        <a-radio-group
          v-decorator="['entryVeto',{rules: [{ required: true, message: '请选择是否为否决项' }]}]">
          <a-radio value="0">否</a-radio>
          <a-radio value="1">是</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label='状态' v-bind="formItemLayout">
        <a-radio-group
          v-decorator="['entryStatus',{rules: [{ required: true, message: '请选择状态' }]}]">
          <a-radio value="0">过期</a-radio>
          <a-radio value="1">可用</a-radio>
        </a-radio-group>
      </a-form-item>
    </a-form>
    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-drawer>
</template>
<script>

const formItemLayout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'EntryEdit',
  props: {
    entryEditVisiable: {
      default: false
    }
  },
  data () {
    return {
      formItemLayout,
      form: this.$form.createForm(this),
      entry: {},
      entryId: '',
      loading: false,
      unitData: []
    }
  },
  computed: {
  },
  methods: {
    onClose () {
      this.loading = false
      this.form.resetFields()
      this.$emit('close')
    },
    setFormValues ({...entry}) {
      this.entryId = entry.entryId
      let fields = ['unitId', 'entryNum', 'entryContent', 'entryScore', 'entryBasis', 'entryVeto', 'entryStatus',
        'unitId']
      Object.keys(entry).forEach((key) => {
        if (fields.indexOf(key) !== -1) {
          this.form.getFieldDecorator(key)
          let obj = {}
          obj[key] = entry[key]
          this.form.setFieldsValue(obj)
        }
      })
    },
    handleSubmit () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true
          let entry = this.form.getFieldsValue()
          entry.entryId = this.entryId
          this.$put('entry', {
            ...entry
          }).then((r) => {
            this.loading = false
            this.$emit('success')
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  },
  watch: {
    entryEditVisiable () {
      if (this.entryEditVisiable) {
        this.$get('unit').then((r) => {
          this.unitData = r.data.rows
        })
      }
    }
  }
}
</script>
